package cn.migu.music.hangupsms.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cn.migu.music.common.utils.LogUtils;
import cn.migu.music.hangupsms.model.CLSmsReportNotifyReq;
import cn.migu.music.hangupsms.model.CLSmsReportNotifyRsp;
import cn.migu.music.hangupsms.model.ViewVrbtCLCache;
import lombok.extern.slf4j.Slf4j;

/**
 * 创蓝短信上报回执接口
 */
@Slf4j
@RestController
public class CLSmsReportService {
    private static final String    RECEIVE_MSG      = "MSG";
    private static final String    STATUS_SUCCESS   = "1";

    private static final String    RSP_SUCCESS_CODE = "1";
    private static final String    RSP_FAIL_CODE    = "0";
    private static final String    RSP_SUCCESS_DESC = "成功";
    private static final String    RSP_FAIL_DESC    = "失败";

    @Autowired
    private ViewVrbtSendMsgService viewVrbtSendMsgService;

    /**
     * 创蓝文本短信上报回执接口
     * 
     * @param clSmsReportNotifyReq
     *            回执通知内容
     * @return 响应参数
     */
    @PostMapping(value = "/hangupsms/cl-sms-report/v1.0")
    public CLSmsReportNotifyRsp clSmsReport(@RequestBody CLSmsReportNotifyReq clSmsReportNotifyReq) {
        CLSmsReportNotifyRsp clSmsReportNotifyRsp = new CLSmsReportNotifyRsp();
        clSmsReportNotifyRsp.setCode(RSP_SUCCESS_CODE);
        clSmsReportNotifyRsp.setMsg(RSP_SUCCESS_DESC);
        if (StringUtils.equals(clSmsReportNotifyReq.getReserve(), RECEIVE_MSG)) {
            // 只接收发短信的上报
            boolean result = sendKafkaForCLReport(clSmsReportNotifyReq);
            if (!result) {
                clSmsReportNotifyRsp.setCode(RSP_FAIL_CODE);
                clSmsReportNotifyRsp.setMsg(RSP_FAIL_DESC);
            }
        }
        return clSmsReportNotifyRsp;
    }

    /**
     * 创蓝视频短信上报回执接口
     *
     * @param clSmsReportNotifyReq
     *            回执通知内容
     * @return 响应参数
     */
    @PostMapping(value = "/hangupsms/cl-vms-report/v1.0")
    public CLSmsReportNotifyRsp clVmsReport(@RequestBody CLSmsReportNotifyReq clSmsReportNotifyReq) {
        CLSmsReportNotifyRsp clSmsReportNotifyRsp = new CLSmsReportNotifyRsp();
        clSmsReportNotifyRsp.setCode(RSP_SUCCESS_CODE);
        clSmsReportNotifyRsp.setMsg(RSP_SUCCESS_DESC);
        // 通知宣发结果
        boolean result = sendKafkaForCLReport(clSmsReportNotifyReq);
        if (!result) {
            clSmsReportNotifyRsp.setCode(RSP_FAIL_CODE);
            clSmsReportNotifyRsp.setMsg(RSP_FAIL_DESC);
        }
        return clSmsReportNotifyRsp;
    }

    private boolean sendKafkaForCLReport(CLSmsReportNotifyReq param) {
        boolean result = false;
        if (!StringUtils.equals(param.getStatus(), STATUS_SUCCESS) || StringUtils.isEmpty(param.getBatchId())) {
            LogUtils.warn(log, "创蓝上报结果为失败:phone:%s,batchId:%s,reportDesc:%s", param.getMobile(), param.getBatchId(), param.getReportDesc());
        } else {
            result = true;
        }

        try {
            ViewVrbtCLCache viewVrbtCLCache = viewVrbtSendMsgService.getCacheCLSendParam(param.getBatchId());
            if (viewVrbtCLCache == null) {
                LogUtils.warn(log, "创蓝发短信redis缓存不存在，batchId:%s,msisdn:%s", param.getBatchId(), param.getMobile());
                return true;
            }
            // 结果发送kafka通知宣发平台
            viewVrbtSendMsgService.sendKafkaForResult(viewVrbtCLCache.getToneId(), param.getMobile(), viewVrbtCLCache.getEndTime(), result,
                viewVrbtCLCache.getMsg());
            // 发送成功，缓存redis记录
            viewVrbtSendMsgService.cacheSendRecordByMsisdn(viewVrbtCLCache.getToneId(), param.getMobile(), result);
            return true;
        } catch (Exception ex) {
            LogUtils.error(ex, log, "创蓝发短信结果通知宣发失败: phone:%s,status:%s,batchId:%s", param.getMobile(), param.getStatus(), param.getBatchId());
        }
        return false;
    }
}
